package cn.stylefeng.guns.modular.rest;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.core.util.CangGuanRoleId;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * 用户
 */
@Controller
@RequestMapping("/rest/auth")
public class authController extends BaseController {

    @Autowired
    private UserAop userAop;
    
    @Autowired
    private UserService userService;

    /**
     * 查询公告
     */
    @RequestMapping("/notice")
    @ResponseBody
    public ResponseData notice() {
    	User user = userAop.getUser();
    	if(user.getRoleId().equals("1424001752635002881")) {
    		if(user.getOperateId() != null) {
    			if(user.getOperateId().equals(38l)||user.getOperateId().equals(39l)||user.getOperateId().equals(40l)||user.getOperateId().equals(42l)||user.getOperateId().equals(43l)||user.getOperateId().equals(44l)
    					||user.getOperateId().equals(83l)||user.getOperateId().equals(86l)) {
    				notice notice = new notice();
    				notice.setTitle("好想来零食温馨提醒");
    				String content = "1、到仓前一小时告知调度员、车队长到仓时间;<br>";
    				content += "2、穿安全马甲进入库区，,专人引领进行装车;<br>";
    				content += "3、监督装车即大不压小重不压轻，多家店的货要用网分割好;少了坏了要赔哟~;<br>";
    				content += "4、发车时请提前联系收货方，请文明礼貌预约;<br>";
    				content += "5、18: 30-21:00 为门店销售高峰期不得强制要求卸货;<br>";
    				content += "6、回单签收字迹清晰,时效3天，若邮寄及时告知调度员快递单号;<br>";
    				content += "7、卸完一家货以后一定要降高平货后再出发;<br>";
    				content += "8、路上遇堵车、故障、极端恶劣天气等异常情况及时向调度员/车队长电话报备;<br>";
    				content += "9、到店需要协助卸货做车尾交接;<br>";
    				notice.setContent(content);
    		        return ResponseData.success(notice);
    			}
    		}
    	}
        return ResponseData.error("");
    }

    /**
     * 获取用户信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/me")
    public ResponseData me() {
        User user = userAop.getUser();
        return ResponseData.success(user);
    }

    /**
     * 获取小程序菜单
     * @return
     */
    @ResponseBody
    @RequestMapping("/getMenu")
    public ResponseData getMenu() {
        User user = userAop.getUser();
        List<Long> roleList = new ArrayList<Long>();
		String[] split = user.getRoleId().split(",");//以逗号分割
        for (String string2 : split) {
        	roleList.add(Long.parseLong(string2));
        }
        return ResponseData.success(userService.getAppletsRoleMenulist(roleList));
    }

    /**
     * 查询是否是组长、配送角色
     * @return
     */
    @ResponseBody
    @RequestMapping("/photo")
    public ResponseData photo() {
        User user = userAop.getUser();
		String[] split = user.getRoleId().split(",");//以逗号分割
		if(CangGuanRoleId.ZZRoleId.longValue() != 0l) {
			for (String string2 : split) {
				Long zzRole = CangGuanRoleId.ZZRoleId;
				Long psRole = CangGuanRoleId.PSRoleId;
				Long userR = Long.parseLong(string2);
				if (userR.longValue() == zzRole.longValue()) {
					return ResponseData.success(false);
				}
				if (userR.longValue() == psRole.longValue()) {
					return ResponseData.success(false);
				}
			}
		}
		return ResponseData.success(true);
    }
    
    /**
     * 修改当前用户的密码
     */
    @RequestMapping("/changePwd")
    @ResponseBody
    public ResponseData changePwd(@RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword) {
        User user = userAop.getUser();

        String oldMd5 = ShiroKit.md5(oldPassword, user.getSalt());

        if (user.getPassword().equals(oldMd5)) {
            String newMd5 = ShiroKit.md5(newPassword, user.getSalt());
            user.setPassword(newMd5);
            userService.updateById(user);
        } else {
            return ResponseData.error("原密码不正确");
        }
        return ResponseData.success();
    }

    @RequestMapping("/changeBaseInfo")
    @ResponseBody
    public ResponseData changeBaseInfo(User param) {
        User user = userAop.getUser();

        if (StringUtils.isNotBlank(param.getName())) {
            user.setName(param.getName());
        }
        if (StringUtils.isNotBlank(param.getPhone())) {
            user.setPhone(param.getPhone());
        }
        userService.updateById(user);
        return ResponseData.success();
    }
}
